//
//  ThingsimgrankTableViewController.m
//  糗事百科
//
//  Created by lanou on 16/5/28.
//  Copyright © 2016年 刘恒春. All rights reserved.
//


//纯图页面

#import "ThingsimgrankTableViewController.h"
#import "RequestManager.h"
#import "ThingsListModel.h"
#import "ThingsimageTableViewCell.h"
#import "timeTool.h"
#import "ThingsInfoViewController.h"
@interface ThingsimgrankTableViewController ()

@property (nonatomic,strong) NSMutableArray *datasource;
@property (nonatomic,assign)CGFloat strh;
@property (nonatomic,assign) NSInteger page;
@end

@implementation ThingsimgrankTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //数组初始化
    _datasource = [NSMutableArray array];
    //网络请求
       _page = 1;
    [self netrequset];
    [self.tableView registerNib:[UINib nibWithNibName:@"ThingsimageTableViewCell" bundle:nil] forCellReuseIdentifier:@"imagecell"];
    //上拉and下拉刷新
    
    // 下拉刷新数据
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^
    {
        // 下拉刷新数据 需要重新网络请求
        _page= 1;
        [_datasource removeAllObjects];
        [self netrequset];
    }];
    
    //上拉加载更多
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^
    {
        _page = _page+1;
        //再次请求数据
        [self netrequset];
    }];

}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _datasource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ThingsimageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"imagecell" forIndexPath:indexPath];
    cell.layer.cornerRadius = 8;
    cell.layer.masksToBounds = YES;
    ThingsListModel  *model = _datasource[indexPath.row];
    
    NSString *idstring = [NSString stringWithFormat:@"%@",model.user.uid];
    NSString *substring = [idstring substringToIndex:idstring.length/2];
    NSString *urlstring = [NSString stringWithFormat:@"%@/%@/medium/%@",substring,idstring,model.user.icon];
    
    
    NSString *imastr = [model.image substringWithRange:NSMakeRange(3,9)];
    NSString *subimastr = [model.image substringWithRange:NSMakeRange(3,5)];
    NSString *imaurlstr = [NSString stringWithFormat:@"%@/%@/medium/%@",subimastr,imastr,model.image];
  
    NSNumber *like =[NSNumber numberWithInt:[model.votes.up intValue] + [model.votes.down intValue]];
    
    
    if ([model.type isEqualToString:@"hot"]) {
        cell.imaright.image =[UIImage imageNamed:@"hot"];
    }else if([model.type isEqualToString:@"fresh"])
    {
        cell.imaright.image = [UIImage imageNamed:@"fresh"];
    }
    
    if (model.user.login ==NULL)
    {
        cell.imaicon.image = [UIImage imageNamed:@"jijun"];
        cell.imauname.text = @"匿名用户";
        
    }
    [cell.imaicon sd_setImageWithURL:[NSURL URLWithString:[kiconurl stringByAppendingString:urlstring]]];
    cell.imauname.text = model.user.login;
    cell.imatime.text = [[timeTool alloc] publishtime:[NSString stringWithFormat:@"%@",model.published_at]];
    cell.imacontent.text = model.content;
    [cell.imapic sd_setImageWithURL:[NSURL URLWithString:[kimaurl stringByAppendingString:imaurlstr]]];
    cell.imapic.frame =CGRectMake(10, 80+_strh, [model.image_size.s[0] floatValue], [model.image_size.s[1] floatValue]);
    cell.likecout.text = [NSString stringWithFormat:@"%@",like];
    cell.commmentcount.text =[NSString stringWithFormat:@"%@",model.comments_count];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ThingsListModel  *model = _datasource[indexPath.row];
    _strh =  [self heightForString:model.content size:14];
    NSArray *arr=model.image_size.s;
    NSNumber *ima =arr[1];
    CGFloat imah =[ima floatValue];
    return _strh+160+imah;
}
- (CGFloat)heightForString:(NSString *)aString size:(NSInteger)size{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:size]};
    CGRect rect = [aString boundingRectWithSize:CGSizeMake(300, 100000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}
-(void)netrequset
{
    [RequestManager initWithUrlString:[NSString stringWithFormat:@"%@?page=%ld",_imgrankUrl,(long)_page] requestType:@"GET" parDic:nil finishBlock:^(NSData *data)
     {
        
         NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
         NSArray *listArray = dataDic[@"items"];
         
         for (NSDictionary *dic in listArray)
         {
             ThingsListModel *model =[[ThingsListModel alloc]init];
             [model setValuesForKeysWithDictionary:dic];
             [_datasource  addObject:model];
         }
         dispatch_async(dispatch_get_main_queue(), ^
        {
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            
        });
         
         NSLog(@"纯图请求成功");
         
     } errorBlock:^(NSError *error)
     {
         NSLog(@"纯图请求失败");
     }];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ThingsListModel *model =_datasource[indexPath.row];
    NSString *idstring = [NSString stringWithFormat:@"%@",model.user.uid];
    NSString *substring = [idstring substringToIndex:idstring.length/2];
    NSString *urlstring = [NSString stringWithFormat:@"%@/%@/medium/%@",substring,idstring,model.user.icon];
    
    NSString *imastr = [model.image substringWithRange:NSMakeRange(3,9)];
    NSString *subimastr = [model.image substringWithRange:NSMakeRange(3,5)];
    NSString *imaurlstr = [NSString stringWithFormat:@"%@/%@/medium/%@",subimastr,imastr,model.image];

    ThingsInfoViewController *infoVc = [[ThingsInfoViewController alloc ] init];
    infoVc.infoid = model.ID;
    infoVc.infoicon = [kiconurl stringByAppendingString:urlstring];
    infoVc.infouname = model.user.login;
    infoVc.infocontent = model.content;
    infoVc.infolikecount = [NSString stringWithFormat:@"%@",[NSNumber numberWithInt:[model.votes.up intValue] + [model.votes.down intValue]]];
    infoVc.infocommmentcout=[NSString stringWithFormat:@"%@",model.comments_count];
    infoVc.infoima =[kimaurl stringByAppendingString:imaurlstr];
     infoVc.type = model.format;

    infoVc.imah = [model.image_size.s[1] floatValue];
    infoVc.imaw = [model.image_size.s[0] floatValue];
    [self.navigationController pushViewController:infoVc animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}

@end
